<template>
  <div class="comment-container" v-loading="loading">
    <el-table :data="data" style="width: 100%" border>
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          {{ scope.$index + (currentPage - 1) * ecahPage + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="头像" width="250" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 200px"
            fit="contain"
            :preview-src-list="srcList"
            :src="scope.row.avatar1"
          />
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" align="center" />
      <el-table-column label="评论文章" width="280" align="center">
        <template slot-scope="scope">
          {{ scope.row.blog.title }}
        </template>
      </el-table-column>
      <el-table-column prop="content" label="评论内容" align="center" />
      <el-table-column
        label="创建日期"
        align="center"
        width="300"
        prop="createDate"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            transition="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="handleDel(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next, total, ->, sizes, jumper"
      :current-page.sync="currentPage"
      :page-size.sync="ecahPage"
      :page-sizes="[5, 10, 20]"
      :total="count"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="hanlePreClick"
      @next-click="handleNextClick"
    />
  </div>
</template>

<script>
import { getComment, delComment } from "@/api/comment";
import { formDate } from "@/utils/tools";
import { server_URL, frontEnd_URL } from "@/urlConfig";

export default {
  name: "Comment",
  data() {
    return {
      data: [],
      srcList: [], // 预览图
      currentPage: 1, // 当前页
      ecahPage: 5, // 每页多少
      totalPage: 0, // 一页多少个
      count: 0, // 总数
      // pageCurrentPage: 1,
      loading: false,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.loading = true;
      getComment(this.currentPage, this.ecahPage)
        .then((res) => {
          this.data = res.data.rows.map((v) => ({
            ...v,
            avatar1: server_URL + v.avatar,
            createDate: formDate(v.createDate),
          }));
          this.srcList = res.data.rows.map((v) => server_URL + v.avatar);
          this.count = res.data.total;
          this.totalPage = Math.ceil(this.count / this.ecahPage);
          if (this.currentPage > this.totalPage) {
            this.currentPage = this.totalPage;
            this.fetchData();
          }
        })
        .finally(() => (this.loading = false));
    },
    goToTitleHandler(blogInfo) {
      window.open(`${frontEnd_URL}/article/${blogInfo.id}`);
    },
    handleDel(row) {
      this.$confirm(
        "删除该文章会见下面的评论一并删除, 是否继续?",
        "是否删除此片文章",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          delComment(row.id).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(pageSize) {
      this.ecahPage = +pageSize;
      this.currentPage = 1;
      // this.pageCurrentPage = 1;
      this.fetchData();
    },
    handleCurrentChange(pageNumber) {
      this.currentPage = +pageNumber;
      this.fetchData();
    },
    hanlePreClick() {
      this.currentPage -= 1;
    },
    handleNextClick() {
      this.currentPage += 1;
    },
  },
};
</script>

<style scoped lang="scss">
.comment-container {
  padding: 20px;
  .el-pagination {
    margin-top: 10px;
  }
}
</style>
